<template>
	<view class="u-page">
		<u-subsection :list="list" :current="currentIndex" @change="clickTab"></u-subsection>
		<view class="total" v-if="currentIndex==0">
			<u-cell-group>
				<u-cell v-for="item in total" :title="item.goods.name+' - '+item.spec.name" :value="item.total_number+item.spec.unit">
				</u-cell>
			</u-cell-group>
			<u-empty v-if="total.length==0" text="今日没有订单!" mode="search" marginTop="150"></u-empty>
		</view>
		<view v-if="currentIndex==1">
			<u-empty v-if="orders.length==0" text="今日没有订单!" mode="search" marginTop="150"></u-empty>
			<block v-else>
				<view style="padding:10rpx;">
					<orderlist :orders="orders" :init_status="2" day="today" v-on:updateList="updateList"/>
				</view>			
			</block>
		</view>	
		<driverbar name="today"/>
	</view>
</template>
<script>
	import Orderlist from './orderlist.vue';
	import Driverbar from './driverbar.vue';
	import {methods} from '@/utils/methods.js';
	let interval;
	export default {
		components: {
			Orderlist,Driverbar
		},
		data() {
			return {
				height: 0,
				list: ['配送汇总','店铺'],
				currentIndex: 1,
				latitude: '26.6015',
				longitude: '106.62254',
				//订单
				markers: [],
				orders: [],
				total:[]
			}
		},
		onUnload: function() {
			if (interval) {
				clearInterval(interval);
				interval = '';
			}
		},
		onLoad(options) {
			let that = this;
			uni.getSystemInfo({
				success(res) {
					that.height = res.windowHeight;
				}
			});
		},
		onShow() {
			let that = this;
			uni.getLocation({
				type: 'gcj02',
				success(res) {
					that.longitude = res.longitude;
					that.latitude = res.latitude;
					that.getOrderList();
				},
				fail(e) {
					that.longitude = 106.62254;
					that.latitude = 26.6015;
					that.getOrderList();
				}
			});
			if (!interval) {
				interval = setInterval(function() {
					that.getOrderList();
				}, 60000);
			}
		},
		methods: {
			...methods,
			clickTab(params) {
				this.currentIndex = params;	
				if(this.currentIndex==0){
					this.$get('driver/total').then(res=>{
						this.total=res;
					});
				}		
			},
			updateList:function(){
				this.getOrderList();
			},
			getOrderList() {
				this.$get('driver/order',{
					type:'today',
					latitude: this.latitude,
					longitude: this.longitude
				}).then(info=>{
					this.orders = info;
					let markers = [];
					let lastpoint = {};
					for (let i = 0; i < info.length; i++) {
						let position = info[i].position;
						let list = info[i].list;
						if(!list){
							continue;
						}
						let all = 0;
						for (let j = 0; j < list.length; j++) {
							if (list[j].receive_time) {								
								all++;
							}
						}
						let iconPath = '/static/driver/qs_active.png';
						let color='#d11111';
						if (list.length == all) {
							iconPath = '/static/driver/qs_success.png';
							color="#42b983";
						}
						if (all > 0 && list.length > all) {
							iconPath = '/static/driver/qs_take.png';
							color="#ecdb0b";
						}
						let marker = {
							id: i,
							latitude: position.latitude,
							longitude: position.longitude,
							label:{
								content:position.title,
								fontSize:12,
								color:color,
								anchorX:0,
								anchorY:-40,
								textAlign:'center'
							},
							iconPath: iconPath,
							width: 30,
							height: 20
						};
						markers.push(marker);
					}
					this.markers = markers;
				});
			}
		}
	}
</script>
<style scoped lang="scss">
.total{
	
}
</style>
